﻿using System;
using Microsoft.Practices.ObjectBuilder;
using ModulesToolkit.DataContracts;
using ModulesToolkit.DataContracts.Membership;
using System.Linq;
using Microsoft.Practices.Web.UI.WebControls;

namespace ModulesToolkit.Membership.Views
{
    public partial class Rights : Microsoft.Practices.CompositeWeb.Web.UI.Page, IRightsView
    {
        #region Fields
        private RightsPresenter _presenter;
       // private ScreenRightDtoSearchCriteria SearchCriteria;
        #endregion Fields

        #region Properties


        #endregion Properties

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized(); 
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public RightsPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        public void FilluxScreensList(EntitySearchResult<Screens> TotalScreens)
        {

            uxScreensList.DataSource = TotalScreens.Items;
            uxScreensList.DataTextField = "Name";
            uxScreensList.DataValueField = "Id";
            uxScreensList.DataBind();

        }

        public void FilluxRightsList(EntitySearchResult<ScreenRightsDto> TotalRights)
        {
            uxRightsListSource.DataSource = TotalRights;
           // uxRightsListSource.DataBind();
        }

        public Int32? GetSelectedValue()
        {
            if (uxScreensList.SelectedValue == "")
            {
                return null;
            }
            else
            {
                Int32 Selected = Convert.ToInt32(uxScreensList.SelectedValue);
                return Selected;
            }
        }

        public void uxScreensList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            this._presenter.OnSelectedIndexChanged();
        }

        public void uxRightsList_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        {
            int pageIndex = this.uxRightsList.PageIndex + 1;
            int pageSize = this.uxRightsList.PageSize;
            string sortField = e.GetSortExpression();
            string sortDirection = e.GetSortDirection();
            this._presenter.OnLoadRightsList(pageIndex, pageSize, sortField, sortDirection);
        }       

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


    }
}

